<?php
/** 
 * Created by RisingSun. 
 * User: MarkSpace 
 * Date: 20210822
 * Time: 11:05 
 * 产品中心管理
 */
namespace app\product\controller;

use  Mscore\Core\Controller;
use  Mscore\Core\Log;
use  Mscore\Core\Lib\Upload;
use  Mscore\Core\Lang;
use  app\admin\model\Login as LoginModel;

class CommonController extends Controller{
   
    public $login;
    private $oss=false;
    /**
     * 初始化自动连接方法
     * @param array $sysmod 
     * @param array $where 
    */
    public function __construct()  
    {    
        $this->header =get_all_headers();
        $this->header['language']=isset($this->header['language'])?$this->header['language']:'zh';
        Lang::init($this->header['language']);
        $this->success=Lang::get('Success');
        $this->error=Lang::get('Error');
    }
    
    /**
     * 编辑器上传图片
     * @param array $editor_upload_img 
     * @param array $where 
     */
    public function editor_upload_img($file='file'){
      $user = (new LoginModel)->isAuth();
      $icon ='';
      $url= 'static/upload/'.$file.'/'.date('Ym',time());
      if(!is_dir(config('file.path').$url)){
         mkdir(config('file.path').$url, 0777, true); 
      }
      if(!isset($_FILES['file'])){
          return $this->echojson(Lang::get('Uploadempty'));
      }
      $icon = (new Upload)->Uploadfile($_FILES['file'],$url);
      Log::write(sprintf("upload aid=【%s】,name=【%s】,file=【%s】", $user['aid'], $user['username'], $icon),'upfile/',date("y_m_d").".log");
      $this->success['data']=['src'=>'/'.$icon];
      return $this->echojson($this->success);
    }

    /**
     * 上传图片
     * @param array $upload 
     * @param array $where 
     */
    public function uploadimg(){
      if($this->cross['crossdomain']){
        foreach ($this->cross['crossinfo'] as $key => $value) {
          header($value);
        }
      }
      $folder=get();
      $user = (new LoginModel)->isAuth()['info'];
      $icon ='';
      $hoder = isset($folder['folder'])?$folder['folder']:'file';
      $aid = isset($folder['name'])?$folder['name']:$user['aid'];
      $url='static/upload/'.$hoder.'/'.date('Ym',time());
      if(!is_dir(config('file.path').$url)){
          mkdir(config('file.path').$url, 0777, true); 
      }
      if(!isset($_FILES['file'])){
          return $this->echojson(Lang::get('Uploadempty'));
      }
      $icon = (new Upload)->Uploadimg($_FILES['file'],$url,'back'.$aid);
      Log::write(sprintf("upload aid=【%s】,name=【%s】,file=【%s】", $user['aid'], $user['username'], $icon),'upfile/',date("y_m_d").".log");
      $this->success['data']=['src'=>'/'.$icon];
      return $this->echojson($this->success);
    }

}